package com.car.service.car;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.car.dao.car.CarDao;
import com.car.dao.department.DepartmentDao;
import com.car.dao.user.UserDao;
import com.car.entity.Car;
import com.car.entity.Department;
import com.car.entity.User;
import com.car.util.ResultMsg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * @ClassName: CarServiceImpl
 * @Description:
 * @Author 余全盛
 * @Date: 2021/12/2 17:49
 */
@Service
public class CarServiceImpl implements CarService {
    @Autowired
    private CarDao carDao;

    @Autowired
    private UserDao userDao;

    @Autowired
    private DepartmentDao departmentDao;

    @Override
    public List<Car> getAllUsableCars(int departmentId) {
        LambdaQueryWrapper<Car> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Car::getCarState, 0);
        wrapper.eq(Car::getDepartmentId,departmentId);
        return carDao.selectList(wrapper);
    }

    @Override
    public Car getCarByID(int carId) {
        return carDao.selectById(carId);
    }

    @Override
    public int updateCarState(int carId, int carState) {
        LambdaUpdateWrapper<Car> wrapper = new LambdaUpdateWrapper();
        wrapper.eq(Car::getCarId, carId);
        wrapper.set(Car::getCarState, carState);
        return carDao.update(null, wrapper);
    }

    //gh->
    @Override
    public Page selectLikeCar(Map<String, Object> map) {
        Integer pageNum = (Integer) map.get("pageNum");
        Integer pageSize = (Integer) map.get("pageSize");
        Page page = new Page((pageNum-1)*pageSize,pageSize);

        Integer pageIndex = (pageNum-1)*pageSize;
        map.put("pageIndex",pageIndex);
        Integer carCount = carDao.getCarCount(map);
        //设置起始位置
        List list = carDao.selectLikeCar(map);
        //先设置总条数
        page.setTotal(carCount);
        //设置每页大小
        page.setSize(pageSize);
        //设置当前页
        page.setCurrent(pageNum);
        //设置内容
        page.setRecords(list);
        return page;
    }

    @Override
    public ResultMsg addCar(Car car, String userName, String departmentName) {
        ResultMsg resultMsg = new ResultMsg();
        User addUser = userDao.getUserByName(userName);
        Department addDepartment = departmentDao.getDepartmentByName(departmentName);
        car.setUserId(addUser.getUserId());
        car.setDepartmentId(addDepartment.getDepartmentId());
        int flag = carDao.insert(car);
        if (flag > 0) {
            resultMsg.setMsg("添加成功");
        } else {
            resultMsg.setMsg("添加失败");
        }
        resultMsg.setFlag(flag);
        return resultMsg;
    }

    @Override
    public ResultMsg delCar(Car car) {
        ResultMsg resultMsg = new ResultMsg();
        LambdaUpdateWrapper<Car> wrapper=new LambdaUpdateWrapper();
        wrapper.eq(Car::getCarId,car.getCarId());
        wrapper.set(Car::getCarState,car.getCarState());
        int flag = carDao.update(null,wrapper);
        if (flag > 0) {
            resultMsg.setMsg("删除成功");
        } else {
            resultMsg.setMsg("删除失败");
        }
        resultMsg.setFlag(flag);
        return resultMsg;
    }

    @Override
    public ResultMsg updateCar(Car car, String userName, String departmentName) {
        ResultMsg resultMsg = new ResultMsg();
        User addUser = userDao.getUserByName(userName);
        Department addDepartment = departmentDao.getDepartmentByName(departmentName);
        car.setUserId(addUser.getUserId());
        car.setDepartmentId(addDepartment.getDepartmentId());
        int flag = carDao.updateById(car);
        if (flag > 0) {
            resultMsg.setMsg("修改成功");
        } else {
            resultMsg.setMsg("修改失败");
        }
        resultMsg.setFlag(flag);
        return resultMsg;
    }

    @Override
    public ResultMsg selectByCarId(Car car) {
        ResultMsg resultMsg = new ResultMsg();
        Car result = carDao.getCarById(car);
        if (result != null) {
            resultMsg.setMsg("查询成功");
            resultMsg.setFlag(1);
            resultMsg.setEntityObj(result);
        } else {
            resultMsg.setMsg("查询失败");
            resultMsg.setFlag(0);
        }
        return resultMsg;
    }
    //<-gh
}
